//
// Created by jiaoheng on 2024/11/13.
//
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "shop.h"


// 产品数组
Product **products;
int products_capacity = 5;
int products_size = 0;

// 初始化产品数组
void initProducts(){
    if(products==NULL){
        printf("开始初始化产品数组");
        products = malloc(products_capacity * sizeof(Product *));
    }else{
        printf("已经初始化，跳过初始化产品数组");
    }

}

// 将字符串转换为产品类型
ProductType stringToProductType(char *str){
    if(strcmp(str,"BOOK")==0){
        return BOOK;
    }else if(strcmp(str,"CLOTHING")==0){
        return CLOTHING;
    }else if(strcmp(str,"ELECTRONICS")==0){
        return ELECTRONICS;
    }else if(strcmp(str,"FOOD")==0){
        return FOOD;
    }else{
        return UNKNOWN;
    }
}

char *productNames[]={
        "BOOK",
        "ELECTRONICS",
        "CLOTHING",
        "FOOD",
        "UNKNOWN"
};
// 根据产品值转换为产品类型名称
char *productTypeToString(ProductType type){
    return productNames[type];
}
// 展示产品
void showProducts(){
    // 输出表头
    printf("|%-10s\t|%-10s\t|%-10s\t|%-10s|\n","id","名称","库存","类型");
    printf("--------------------------------------------------\n");
    for(int i=0;i<products_size;i++){
        char * productType = productTypeToString(products[i]->type);
        printf("|%-10d\t|%-10s\t|%-10d\t|%-10s|\n",products[i]->id,products[i]->name,products[i]->quantity,productType);
    }
    printf("--------------------------------------------------\n");
}

// 添加产品
void addProduct(){
    if(products_capacity<=products_size){
        printf("产品数组已满，正在扩容...\n");
        products_capacity = products_capacity*2;
        products = realloc(products,products_capacity*sizeof(Product*));
    }
    printf("请输入产品信息，逗号分割\n");
    fflush(stdout);
    fflush(stdin);
    char param[50];
    //fgets(param, 50, stdin); // 使用 fgets 替代 scanf 避免缓冲区溢出
    scanf("%s", param);
    // 解析参数
    char *id_str = strtok(param,",");
    char *name_str = strtok(NULL,",");
    char *quantity_str = strtok(NULL,",");
    char *type_str = strtok(NULL,",");
    // 转换参数
    int id = atoi(id_str);
    int quantity = atoi(quantity_str);
    ProductType type = stringToProductType(type_str);
    char* name = malloc(strlen(name_str)+1);
    strcpy(name,name_str);
    // 创建产品
    Product *product = malloc(sizeof(Product));
    product->id = id;
    product->name = name;
    product->quantity = quantity;
    product->type = type;

    products[products_size] = product;
    products_size++;
    printf("添加成功\n");
}

void updateProduct(){
    printf("请输入产品信息，逗号分割\n");
    fflush(stdout);
    fflush(stdin);
    char param[50];
    //fgets(param, 50, stdin); // 使用 fgets 替代 scanf 避免缓冲区溢出
    scanf("%s", param);
    // 解析参数
    char *id_str = strtok(param,",");
    char *name_str = strtok(NULL,",");
    char *quantity_str = strtok(NULL,",");
    char *type_str = strtok(NULL,",");
    // 转换参数
    int id = atoi(id_str);
    int quantity = atoi(quantity_str);
    ProductType type = stringToProductType(type_str);
    char* name = malloc(strlen(name_str)+1);
    strcpy(name,name_str);
    // 根据id更新产品
    for(int i=0;i<products_size;i++){
        if(products[i]->id==id){
            products[i]->name = name;
            products[i]->quantity = quantity;
            products[i]->type = type;
            printf("更新成功\n");
            return;
        }
    }
    printf("更新失败，未找到对应记录\n");
}

void deleteProduct(){
    printf("请输入产品id\n");
    fflush(stdout);
    fflush(stdin);
    char param[50];
    //fgets(param, 50, stdin); // 使用 fgets 替代 scanf 避免缓冲区溢出
    scanf("%s", param);
    // 转换参数
    int id = atoi(param);
    // 根据id删除产品
    for(int i=0;i<products_size;i++){
        if(products[i]->id==id){
            free(products[i]);
            memmove(products+i,products+i+1,sizeof(Product*)*(products_size-i-1));
            products_size--;
        }
    }
}
void shop(){
    initProducts();
    int function_num;
    while(1){
        printf("请选择功能:\n");
        printf("1.展示产品 ");
        printf("2.新增产品 ");
        printf("3.更新产品 ");
        printf("4.删除产品 ");
        printf("5.退出\n");
        // windows环境下需要
        fflush(stdout);
        scanf("%d",&function_num);
        if(1==function_num){
            showProducts();
        }else if(2==function_num){
            addProduct();
        }else if(3==function_num){
            updateProduct();
        }else if(4==function_num){
            deleteProduct();
        }else if(5==function_num){
            break;
        }else{
            printf("指令错误请重新输入\n");
        }
    }
}

